/**
 *    @project:    kybernetes
 *    @author:    D.Toepel
 *    @note:        ---
 *
 *    Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package dt.htwk.core;

import java.awt.Font;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;


public class KCClassLoader {

	private static String[] args;
	private static final Class[] parameters = new Class[]{URL.class};

	
	public KCClassLoader(String[] args){
		KCClassLoader.args = args;
		loadClasses();
	}
	
	private static void loadClasses(){
		boolean isNewPath = false;
		for (LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
	    	if ("Nimbus".equals(laf.getName())) {
	    		// in java 7 nimbus will move to javax.swing.plaf.nimbus
	    		String newPath = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
	    		if(newPath == laf.getClassName())
	    			isNewPath = true;
	    	}
	    }
		
		for (int i = 0; i < args.length; i++) {
			if (args[i].matches("-cp")){
				String[] files = args[i+1].split(";");
				for (String file : files) {
					try {
						addFile(file);
						if(Pattern.matches(".\\D*(nimbus)+.*",file.toLowerCase())){
							if(!isNewPath){
								addFile(file);
							}
						}else{
							addFile(file);
						}
					} catch (IOException e) {}
				}
			}
		}
		
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        Toolkit.getDefaultToolkit().setDynamicLayout(true);
        System.setProperty("sun.awt.noerasebackground","true");
        

        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            //new MotifLookAndFeel()
            //new WindowsLookAndFeel()
            //new Plastic3DLookAndFeel()
            //new NimbusLookAndFeel()
            //new MetalLookAndFeel()
        } 
        catch (ClassNotFoundException e) {
        	try {
        		UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			} catch (Exception e2) {
				System.exit(0);
			}
        } 
        
        catch (UnsupportedLookAndFeelException e) {
        	try {
        		UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			} catch (Exception e2) {
				System.exit(0);
			}
        }
        
        catch (Exception e) {
        	System.exit(0);
        }
        
//        Resource res = new Resource();
		UIManager.put("Tree.leafIcon", null);
	    UIManager.put("Tree.openIcon", null);
	    UIManager.put("Tree.closedIcon", null);
//	    UIManager.put("Tree.expandedIcon", res.getExpandedIcon());
//	    UIManager.put("Tree.collapsedIcon", res.getCollapsedIcon());
	    
	}

	private static void addFile(String s) throws IOException {
		File f = new File(s);
		addFile(f);
	}// end method

	private static void addFile(File f) throws IOException {
		addURL(f.toURI().toURL());
	}// end method

	private static void addURL(URL u) throws IOException {
		
		

		URLClassLoader sysloader = (URLClassLoader) ClassLoader
				.getSystemClassLoader();
		Class sysclass = URLClassLoader.class;

		try {
			Method method = sysclass.getDeclaredMethod("addURL", parameters);
			method.setAccessible(true);
			method.invoke(sysloader, new Object[] { u });
		} catch (Throwable t) {
			t.printStackTrace();
			throw new IOException(
					"Error, could not add URL to system classloader");
		}// end try catch
		
		
		

	}// end method
}
